home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / fileinfo.cls < prev    next >
Text File  |  1997-06-14  |  10KB  |  296 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CFileInfo"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Option Explicit
  11.  
  12. Public Enum EErrorFileInfo
  13.     eeBaseFileInfo = 13070      ' CFileInfo
  14. End Enum
  15.  
  16. Public Enum EItemType
  17.     eitFile = 1                 ' File or directory
  18.     eitDrive                    ' Drive
  19.     eitID                       ' PIDL passed to us
  20.     eitFolder                   ' PIDL created by us from special folder
  21. End Enum
  22.  
  23. Private Enum EItemState
  24.     eisNotCreated
  25.     eisFile                     ' File or directory
  26.     eisDrive                    ' Drive
  27.     eisID                       ' PIDL passed to us
  28.     eisFolder                   ' PIDL created by us from special folder
  29. End Enum
  30. Private eis As EItemState       ' How object was created
  31. Private vItem As Variant        ' File name or PIDL
  32. Private shfi As SHFILEINFO      ' Info from SHGetFileInfo
  33. Private fd As WIN32_FIND_DATA   ' Info from FindFirstFile
  34. Private afAttr As Long          ' File attributes
  35. Private afOption As Long        ' Options for SHGetFileInfo
  36.  
  37. Public Enum EExeType
  38.     eetWin16Exe = &H454E
  39.     eetDosExe = &H5A4D
  40.     eetWin32Exe = &H4550
  41.     eetWin32Console = &H4543
  42. End Enum
  43.  
  44. Property Get item() As Variant
  45. Attribute item.VB_UserMemId = 0
  46.     item = vItem
  47. End Property
  48.  
  49. Property Let item(vItemA As Variant)
  50.     Dim h As Long, f As Long, af As Long
  51.     Destroy     ' Clear any previous assignment
  52.     If VarType(vItemA) = vbString Then
  53.         ' String item is a file, directory, or drive
  54.         If Len(vItemA) <= 3 And Mid$(vItemA, 2, 1) = ":" Then
  55.             ' Must be drive, get attributes
  56.             afAttr = 0: afOption = 0
  57.         Else
  58.             ' No terminating backslashes
  59.             MUtility.DenormalizePath vItemA
  60.             ' For file, get information in advance
  61.             h = FindFirstFile(vItemA, fd)
  62.             If h = hInvalid Then ApiRaise Err.LastDllError
  63.             FindClose h
  64.             afAttr = fd.dwFileAttributes
  65.             afOption = SHGFI_USEFILEATTRIBUTES
  66.         End If
  67.         eis = eisFile
  68.         af = afOption And (Not SHGFI_PIDL) Or _
  69.              SHGFI_DISPLAYNAME Or SHGFI_TYPENAME
  70.         f = SHGetFileInfo(vItemA, afAttr, shfi, LenB(shfi), af)
  71.     Else
  72.         ' Integer item is a special folder constant or pidl
  73.         If vItemA < 50 Then
  74.             ' Turn special folder location into a pidl
  75.             Dim pidl As Long
  76.             SHGetSpecialFolderLocation 0, CLng(vItemA), pidl
  77.             vItemA = pidl
  78.             eis = eisFolder
  79.         Else
  80.             eis = eisID
  81.             pidl = vItemA
  82.         End If
  83.         ' For special folders or other PIDLs, everything comes from system
  84.         afAttr = 0: afOption = 0
  85.         ' Get item ID pointer, but don't use attributes
  86.         af = SHGFI_PIDL Or SHGFI_DISPLAYNAME Or _
  87.              SHGFI_TYPENAME
  88.         f = SHGetItemInfo(pidl, afAttr, shfi, Len(shfi), af)
  89.     End If
  90.     If f Then
  91.         vItem = vItemA
  92.     Else
  93.         eis = eisNotCreated
  94.     End If
  95. End Property
  96.  
  97. ' In a drive loop it's more efficient to create from drive data
  98. Sub CreateFromDrive(sRootA As String, sKind As String, _
  99.                     rTotal As Double, rFree As Double)
  100.     Dim f As Long
  101.     Destroy
  102.     afAttr = 0
  103.     fd.dwFileAttributes = 0
  104.     fd.ftLastAccessTime = 0
  105.     fd.ftLastWriteTime = 0
  106.     fd.ftCreationTime = 0
  107.     fd.nFileSizeLow = 0
  108.     fd.dwReserved0 = CLng(rTotal / 1000000)
  109.     fd.dwReserved1 = CLng(rFree / 1000000)
  110.     f = SHGetFileInfo(sRootA, afAttr, shfi, LenB(shfi), _
  111.                       SHGFI_DISPLAYNAME Or SHGFI_TYPENAME Or SHGFI_ATTRIBUTES)
  112.     MBytes.StrToBytes fd.cAlternateFileName, sKind
  113.     If f Then
  114.         vItem = sRootA
  115.         eis = eisDrive
  116.     End If
  117. End Sub
  118.  
  119. ' In a FindFirstFile loop it's more efficient to create from file data
  120. Sub CreateFromFile(sFileA As String, ByVal afAttrA As Long, _
  121.                    ByVal cLenA As Long, ftModifiedA As Currency, _
  122.                    ftAccessedA As Currency, ftCreatedA As Currency)
  123.     Dim f As Long
  124.     Destroy
  125.     afAttr = afAttrA
  126.     fd.dwFileAttributes = afAttrA
  127.     fd.ftLastAccessTime = ftAccessedA
  128.     fd.ftLastWriteTime = ftModifiedA
  129.     fd.ftCreationTime = ftCreatedA
  130.     fd.nFileSizeLow = cLenA
  131.     f = SHGetFileInfo(sFileA, afAttr, shfi, LenB(shfi), _
  132.                       SHGFI_DISPLAYNAME Or SHGFI_TYPENAME Or SHGFI_ATTRIBUTES)
  133.     If f Then
  134.         vItem = sFileA
  135.         eis = eisFile
  136.     End If
  137. End Sub
  138.  
  139. Sub CreateFromNamePidl(sNameA As String, ByVal pidl As Long)
  140.     Dim f As Long, c As Long, s As String
  141.     Destroy
  142.     afAttr = 0
  143.     fd.dwFileAttributes = 0
  144.     fd.ftLastAccessTime = 0
  145.     fd.ftLastWriteTime = 0
  146.     fd.ftCreationTime = 0
  147.     fd.nFileSizeLow = 0
  148.     vItem = pidl
  149.     eis = eisID
  150.     If UnicodeTypeLib Then
  151.         s = sNameA & vbNullChar
  152.         c = Len(s) * 2
  153.         CopyMemoryStr shfi.szDisplayName(0), s, c
  154.     Else
  155.         s = sNameA & vbNullChar
  156.         c = Len(s)
  157.         CopyMemoryStr shfi.szDisplayName(0), s, c
  158.     End If
  159. End Sub
  160.  
  161. Private Sub Destroy()
  162.     ' Free any pidl we created from special folder
  163.     If eis = eisFolder Then Allocator.Free vItem
  164.     eis = eisNotCreated
  165.     vItem = Empty
  166. End Sub
  167.  
  168. Property Get DisplayName() As String
  169.     If eis Then DisplayName = MBytes.ByteZToStr(shfi.szDisplayName)
  170.     If DisplayName = sEmpty Then DisplayName = "Unknown"
  171. End Property
  172.  
  173. ' Returns file type
  174. Property Get TypeName() As String
  175.     If eis Then TypeName = MBytes.ByteZToStr(shfi.szTypeName)
  176.     If TypeName = sEmpty Then TypeName = "None"
  177. End Property
  178.  
  179. Function SmallIcon(Optional afOverlay As Long = 0) As Picture
  180.     Dim shfiT As SHFILEINFO
  181.     If eis = eisNotCreated Then Exit Function
  182.     ' Filter out any invalid flags -- only overlays allowed
  183.     afOverlay = afOverlay And (SHGFI_LINKOVERLAY Or SHGFI_SELECTED _
  184.                                Or SHGFI_OPENICON)
  185.     ' Add in standard and small icon flags
  186.     afOverlay = afOverlay Or afOption Or SHGFI_ICON Or SHGFI_SMALLICON
  187.     GetFileItemInfo vItem, shfiT, afOverlay, afAttr
  188.     Set SmallIcon = MPicTool.IconToPicture(shfiT.hIcon)
  189. End Function
  190.  
  191. Function LargeIcon(Optional afOverlay As Long = 0) As Picture
  192.     Dim shfiT As SHFILEINFO
  193.     If eis = eisNotCreated Then Exit Function
  194.     ' Filter out any invalid flags -- only overlays allowed
  195.     afOverlay = afOverlay And (SHGFI_LINKOVERLAY Or SHGFI_SELECTED _
  196.                                Or SHGFI_OPENICON)
  197.     ' Add in standard and large icon flags
  198.     afOverlay = afOverlay Or afOption Or SHGFI_ICON Or SHGFI_LARGEICON
  199.     GetFileItemInfo vItem, shfiT, afOverlay, afAttr
  200.     Set LargeIcon = MPicTool.IconToPicture(shfiT.hIcon)
  201. End Function
  202.  
  203. Function ShellIcon(Optional afOverlay As Long = 0) As Picture
  204.     Dim shfiT As SHFILEINFO
  205.     If eis = eisNotCreated Then Exit Function
  206.     ' Filter out any invalid flags -- only overlays allowed
  207.     afOverlay = afOverlay And (SHGFI_LINKOVERLAY Or SHGFI_SELECTED _
  208.                                Or SHGFI_OPENICON)
  209.     ' Add in standard and large icon flags
  210.     afOverlay = afOverlay Or afOption Or SHGFI_ICON Or SHGFI_SHELLICONSIZE
  211.     GetFileItemInfo vItem, shfiT, afOverlay, afAttr
  212.     Set ShellIcon = MPicTool.IconToPicture(shfiT.hIcon)
  213. End Function
  214.  
  215. Function Icon(afKind As Long) As Picture
  216.     Dim shfiT As SHFILEINFO
  217.     If eis = eisNotCreated Then Exit Function
  218.     GetFileItemInfo vItem, shfiT, afOption Or SHGFI_ICON Or afKind, afAttr
  219.     Set Icon = MPicTool.IconToPicture(shfiT.hIcon)
  220. End Function
  221.  
  222. ' Retrieves file attribute flags:
  223. '       ReadOnly        Hidden      System      Directory
  224. '       Archive         Normal      Temporary   Compressed
  225. Function Attributes() As Long
  226.     If eis = eisFile Then Attributes = fd.dwFileAttributes
  227. End Function
  228.  
  229. Function length() As Long
  230.     If eis = eisFile Then length = fd.nFileSizeLow
  231. End Function
  232.  
  233. Function Modified() As Date
  234.     If eis = eisFile Then Modified = MFileTool.Win32ToVbTime(fd.ftLastWriteTime)
  235. End Function
  236.  
  237. Function Created() As Date
  238.     If eis = eisFile Then Created = MFileTool.Win32ToVbTime(fd.ftCreationTime)
  239. End Function
  240.  
  241. Function Accessed() As Date
  242.     If eis = eisFile Then Accessed = MFileTool.Win32ToVbTime(fd.ftLastAccessTime)
  243. End Function
  244.  
  245. Function TotalKilo() As Long
  246.     If eis = eisDrive Then TotalKilo = fd.dwReserved0
  247. End Function
  248.  
  249. Function FreeKilo() As Long
  250.     If eis = eisDrive Then FreeKilo = fd.dwReserved1
  251. End Function
  252.  
  253. Function DriveType() As String
  254.     If eis = eisDrive Then DriveType = MBytes.ByteZToStr(fd.cAlternateFileName)
  255. End Function
  256.  
  257. Function ItemType() As EItemType
  258.     ItemType = eis
  259. End Function
  260.  
  261. Private Function GetFileItemInfo(vFileItem As Variant, fi As SHFILEINFO, _
  262.                                  ByVal afOption As Long, _
  263.                                  Optional afAttr As Long = 0) As Long
  264.     Dim f As Long
  265.     If VarType(vFileItem) = vbString Then
  266.         afOption = afOption And (Not SHGFI_PIDL)
  267.         f = SHGetFileInfo(CStr(vFileItem), afAttr, fi, LenB(fi), afOption)
  268.     Else
  269.         afOption = afOption Or SHGFI_PIDL
  270.         f = SHGetItemInfo(CLng(vFileItem), 0, fi, LenB(fi), afOption)
  271.     End If
  272.     GetFileItemInfo = f
  273. End Function
  274.  
  275. #If fComponent = 0 Then
  276. Private Sub ErrRaise(e As Long)
  277.     Dim sText As String, sSource As String
  278.     If e > 1000 Then
  279.         sSource = App.ExeName & ".FileInfo"
  280.         Select Case e
  281.         Case eeBaseFileInfo
  282.             BugAssert True
  283.        ' Case ee...
  284.        '     Add additional errors
  285.         End Select
  286.         Err.Raise COMError(e), sSource, sText
  287.     Else
  288.         ' Raise standard Visual Basic error
  289.         sSource = App.ExeName & ".VBError"
  290.         Err.Raise e, sSource
  291.     End If
  292. End Sub
  293. #End If
  294.  
  295.  
  296.